﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;

    public class PreAllocateRuleDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected Button btnSaveContinue;
        protected CheckBox chbIsActive;
        protected DropDownList ddlPreallocateStrategy;
        protected DropDownList ddlUomID;
        protected HtmlForm F2193;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnType;
        protected Label lblDescr;
        protected Label lblLineID;
        protected Label lblMsg;
        protected Label lblPreAllocateRuleDetail;
        protected Label lblPreAllocateRuleID;
        protected Label lblPreallocateStrategy;
        protected Label lblUomID;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        private List<WMS6.Web.CommonMethodServiceRef.SysCode4DDL> sysPreallocateStrategy;
        private List<WMS6.Web.CommonMethodServiceRef.SysCode4DDL> sysUom;
        protected TextBox txtDesrc;
        protected TextBox txtLineID;
        protected TextBox txtPreAllocateRuleID;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("PreAllocateRule.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtPreAllocateRuleID.Text.Trim())));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool success = false;
            if (string.IsNullOrEmpty(this.txtDesrc.Text.Trim()))
            {
                this.lblMsg.Text = "请输入说明!";
            }
            else
            {
                try
                {
                    PreallocateRuleDetailInfo preallocateRuleDetail = new PreallocateRuleDetailInfo {
                        PreallocateRuleID = this.txtPreAllocateRuleID.Text.Trim(),
                        LineID = int.Parse(this.txtLineID.Text.Trim()),
                        Descr = this.txtDesrc.Text.Trim(),
                        PreallocateStrategy = this.ddlPreallocateStrategy.SelectedValue,
                        UomID = this.ddlUomID.SelectedValue,
                        PreallocatePickCode = "",
                        IsActive = this.chbIsActive.Checked ? "Y" : "N",
                        CreatedBy = base.UserInfo.UserID
                    };
                    if (this.hdnType.Value == "edit")
                    {
                        preallocateRuleDetail.UpdatedBy = base.UserInfo.UserID;
                        if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(preallocateRuleDetail, "PREALLOCATE_RULE_DETAIL")))
                        {
                            success = true;
                        }
                    }
                    else if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(preallocateRuleDetail, "PREALLOCATE_RULE_DETAIL")))
                    {
                        success = true;
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2430_007") + ex.Message;
                    return;
                }
                if (success)
                {
                    base.Response.Redirect(string.Format("PreAllocateRule.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtPreAllocateRuleID.Text.Trim())));
                }
            }
        }

        protected void btnSaveContinue_Click(object sender, EventArgs e)
        {
            bool success = false;
            if (string.IsNullOrEmpty(this.txtDesrc.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2430_006");
            }
            else
            {
                try
                {
                    PreallocateRuleDetailInfo preallocateRuleDetail = new PreallocateRuleDetailInfo {
                        PreallocateRuleID = this.txtPreAllocateRuleID.Text.Trim(),
                        LineID = int.Parse(this.txtLineID.Text.Trim()),
                        Descr = this.txtDesrc.Text.Trim(),
                        PreallocateStrategy = this.ddlPreallocateStrategy.SelectedValue,
                        UomID = this.ddlUomID.SelectedValue,
                        PreallocatePickCode = "",
                        IsActive = this.chbIsActive.Checked ? "Y" : "N",
                        CreatedBy = base.UserInfo.UserID
                    };
                    if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(preallocateRuleDetail, "PREALLOCATE_RULE_DETAIL")))
                    {
                        success = true;
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2430_007") + ex.Message;
                    return;
                }
                if (success)
                {
                    this.lblMsg.Text = this.txtLineID.Text + base.GetGlobalMsgResource("C004");
                    this.txtLineID.Text = (int.Parse(this.txtLineID.Text.Trim()) + 10).ToString();
                    this.txtDesrc.Text = "";
                    if (this.ddlPreallocateStrategy.Items.Count > 0)
                    {
                        this.ddlPreallocateStrategy.SelectedValue = this.ddlPreallocateStrategy.Items[0].Value;
                    }
                    if (this.ddlUomID.Items.Count > 0)
                    {
                        this.ddlUomID.SelectedValue = this.ddlUomID.Items[0].Value;
                    }
                    this.chbIsActive.Checked = true;
                }
            }
        }

        private void InitPreAllocateRuleDetailDropDownLit()
        {
            this.InitPTStrategies(this.ddlPreallocateStrategy);
            this.InitUom(this.ddlUomID);
        }

        private void InitPTStrategies(DropDownList ddl)
        {
            if (this.sysPreallocateStrategy == null)
            {
                this.sysPreallocateStrategy = CommoneMethodServiceClientProxy.GetInstance().GetSysCodeDataSource("PreAllocStrategies");
            }
            ddl.DataSource = this.sysPreallocateStrategy;
            ddl.DataTextField = "SysCodeText";
            ddl.DataValueField = "SysCodeValue";
            ddl.DataBind();
        }

        private void InitUom(DropDownList ddl)
        {
            if (this.sysUom == null)
            {
                this.sysUom = CommoneMethodServiceClientProxy.GetInstance().GetSysCodeDataSource("TUOM");
            }
            ddl.DataSource = this.sysUom;
            ddl.DataTextField = "SysCodeText";
            ddl.DataValueField = "SysCodeValue";
            ddl.DataBind();
        }

        private void LoadData()
        {
            this.InitPreAllocateRuleDetailDropDownLit();
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"].ToString()))
            {
                string PreAllocateRuleID = base.Request.QueryString["ID"].ToString();
                this.txtPreAllocateRuleID.Text = PreAllocateRuleID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"].ToString()))
            {
                string from = base.Request.QueryString["from"].ToString();
                this.hdnFrom.Value = from;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["SeqNO"].ToString()))
            {
                string LineID = base.Request.QueryString["SeqNO"].ToString();
                this.txtLineID.Text = LineID;
            }
            if (base.Request.QueryString["type"] != null)
            {
                this.btnSaveContinue.Enabled = false;
                this.hdnType.Value = "edit";
                this.SetPreAllocateRuleDetail2From();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetPreAllocateRuleDetail2From()
        {
            if ((this.txtPreAllocateRuleID.Text.Trim() != "") && (this.txtLineID.Text.Trim() != ""))
            {
                PreallocateRuleDetailInfo PreallocateRuleDetail = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.txtPreAllocateRuleID.Text.Trim(), this.txtLineID.Text.Trim(), "", "PREALLOCATE_RULE_DETAIL") as PreallocateRuleDetailInfo;
                if (PreallocateRuleDetail != null)
                {
                    this.txtDesrc.Text = PreallocateRuleDetail.Descr;
                    this.ddlPreallocateStrategy.SelectedValue = PreallocateRuleDetail.PreallocateStrategy;
                    this.ddlUomID.SelectedValue = PreallocateRuleDetail.UomID;
                    this.chbIsActive.Checked = PreallocateRuleDetail.IsActive == "Y";
                    this.txtLineID.Enabled = false;
                    this.txtPreAllocateRuleID.Enabled = false;
                }
            }
        }
    }
}

